/*
 * 出库审核
 * 参数   交易码：trancode=51221
 * 
 */

package org.ccams.tran;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.ccams.domain.TMaterielStock;
import org.ccams.domain.TpOutDetail;
import org.ccams.domain.User;
import org.ccams.web.UserSession;
import org.ccams.web.WebBeanFactory;
import org.json.JSONArray;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class Tran51221 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long num =Long.parseLong( request.getParameter("num"));//取得记录ID
		Short ischeck = Short.parseShort(request.getParameter("state"));//取得审核状态
		
		String uname = request.getParameter("uname");
		
		String params = (String) request.getParameter("detail");
		JSONArray detailParams = new JSONArray(params);
		//获得审核人
		HttpSession session = request.getSession();
		UserSession userSession = (UserSession)session.getAttribute("userSession");
		User user = userSession.getUser();
		String userName=user.getUserName();
		//取得当前日期
		Date adate = new Date();
		
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";	
		int k=0;		
		DataSourceTransactionManager txm = (DataSourceTransactionManager) WebBeanFactory.getBean("transactionManager",request);
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = txm.getTransaction(def);
		try{
			TpOutDetail tod = new TpOutDetail();
			tod.setNum(num);
			tod.setAname(userName);
			tod.setIscheck(ischeck);
			tod.setAdate(adate);
			
			k = getCcamsService().getTpoutdetailDao().updateCommit(tod);//更新审核信息
			if(ischeck == 0 && k >= 1){
				for(int i=0;i<detailParams.length();i++){
					org.json.JSONObject detail = detailParams.optJSONObject(i);
					TMaterielStock tms = new TMaterielStock();
//					tms.setUname(uname);
					tms.setUid(detail.getInt("uid"));
//					tms.setMaterielName(detail.getString("materielName"));
					tms.setIdt_materiel(detail.getLong("idt_Materiel"));
					tms.setAmount(0-detail.getLong("amount"));
					
					int stockAmount = getCcamsService().getTmaterielstockDao().getStockAmount(tms);//取得库存数量
					if(stockAmount < detail.getLong("amount")){//进行数量判断
						txm.rollback(status);
						jsonMap.put("success", false);
						jsonMap.put("message", "物料<"+tod.getMaterielName()+">的库存不足");
						json.putAll(jsonMap);
						return;
					}
					k = getCcamsService().getTmaterielstockDao().updateStockAmount(tms);//更新入库数量
				}
			}
		}
		catch(Exception ex){
			txm.rollback(status);
	        ex.printStackTrace();
		}
		txm.commit(status);
		
		if (k >= 1) {
			success = true;
			message = "审核成功";
		} else {
			success = false;
			message = "审核失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
